package com.example.cms.service.Impl;

import com.example.cms.dao.LoginLogDao;
import com.example.cms.dao.UserDao;
import com.example.cms.dao.indexDao;
import com.example.cms.em.ErrorMesgEnum;
import com.example.cms.entity.LoginLog;
import com.example.cms.entity.UserInfo;
import com.example.cms.pojo.BaseModel;
import com.example.cms.pojo.Mesagge;
import com.example.cms.ras.MD5DES;
import com.example.cms.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LoginServiceImpl
 * @Descriptiom TODO
 * @Author Sunbird
 * @Date 2018/12/10 16:36
 * @Version 1.0
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;

    @Autowired
    private indexDao indexDao;

    @Override
    public void login(LoginLog loginLog, BaseModel baseModel) throws Exception{
        //1.根据用户名查询用户信息
        UserInfo user = userDao.selectUserByAccount(loginLog.getUser_name());
        //2.判断帐号是否存在
        if (user == null) {//如果用户信息不存在，说明帐号不存在
            baseModel.setResultCode(1);
            baseModel.setMessage("帐号或密码错误");
            loginLog.setState(2);
        } else {//说明存在帐号
            //验证是否是连续错误登录
            //获取当前时间
            long time =30*60*1000;//半个小时的毫秒数
            //List<LoginLog> log = loginLogDao.selectLoginCount(loginLog);
            //判断是否是连续五次错误
//            if (log.size()>=5){
//                //如果是连续五次错误判断是否在半小时内
//                long currtime =new Date().getTime();
//                //获取第一次登陆失败的时间
//                long logTime= log.get(0).getLogin_date().getTime();
//                //把当前时间转换为时间戳
//                if (currtime-logTime<time){
//                    baseModel.setResultCode(1);
//                    baseModel.setMessage(ErrorMesgEnum.ACCOUT_LOCK.getMesg());
//                    loginLog.setState(5);
//                }
//            }
            //3.验证帐号是否激活
            if (user.getState() == 1) {//说启用
                //4.判断密码是否正确
                if (user.getPassword().equals(MD5DES.encrypt(loginLog.getUser_password()))) {//如果密码正确，登录成功
                    loginLog.setState(1);
                    baseModel.setResultCode(1);
                    baseModel.setMessage("登录成功");
                    user.setPassword(null);//密码等敏感数据不返回
                    baseModel.setData(user);
                    loginLog.setUser_id(user.getUser_id());
                    loginLog.setUser_name(user.getName());
                    loginLog.setUser_password(null);//登录成功的密码不保存在登录日志
                } else {//帐号或密码错误
                    baseModel.setResultCode(0);
                    baseModel.setMessage("帐号或密码错误");
                    loginLog.setState(3);
                }
            } else {//没启用
                loginLog.setState(4);
                baseModel.setResultCode(0);
                baseModel.setMessage("帐号未激活，请联系管理员");
                loginLog.setUser_password(null);//未激活的密码不保存在登录日志
            }
        }
        //5.不管登录成功否，记录登录日志
        int count = loginLogDao.insert(loginLog);
        if (count == 0) {//新增登录日志失败
            baseModel.setResultCode(0);
            baseModel.setMessage("新增登录日志失败");
        }
    }

    @Override
    public void Loginout(LoginLog loginLog, BaseModel baseModel) throws Exception {

    }

    @Override
    public void IndexCount(Mesagge mesagge) {
      List<Map<String,Integer>>  map= indexDao.selectAllCount();
       mesagge.setList(map);
    }
}
